<?php
/**
 * Created by PhpStorm.
 * User: 韩梦雅
 * Date: 2019/2/22
 * Time: 13:42
 */
namespace App\Services\Admin\Invite;
use App\Model\Industry;
use App\Model\Company;
use App\Model\Position;
use App\Services\CommonService;
use Illuminate\Support\Facades\DB;

class IndexService extends CommonService
{
    //查询热门企业
    public function companyList()
    {
        //先查询公司
        $arr = Company::get()->where('is_hot','=','1');
        //定义数组
        $list = array();
        foreach($arr as $k=>$v)
        {
            $cid = $v->id;
            //查询职位表
            //$sql = "SELECT position_name FROM position WHERE company_id='$cid'";
            $list = Position::get()->where('company_id','=',$cid);
            $v->zhiwei = $list;
        }
        return $arr;
    }
    //查询热门行业
    public function industryList()
    {
        $arr = Industry::where('is_hot', 1)
            ->orderBy('rank', 'desc')
            ->get();
        if(empty($arr))
        {
            return json_encode(['code'=>'201','msg'=>'没有满足要求的数据']);
        }
        if(!isset($arr))
        {
            return json_encode(['code'=>'500','msg'=>'SQL语句有误']);
        }
        return $arr;
    }
    //行业、公司、职位的搜索
    public function inviteSearch($sou)
    {
        $arr =  DB::table('position')
            ->join('company', 'position.company_id', '=', 'company.id')
            ->join('industry', 'position.industry_id', '=', 'industry.industry_id')
            ->where('position.position_name','like',"%$sou%")
            ->orWhere('company.name','like',"%$sou%")
            ->orWhere('industry.name','like',"%$sou%")
            ->get();

       /* if(empty($arr))
        {
            return json_encode(['code'=>'201','msg'=>'暂时没有满足您需求的商品']);
        }*/
        return $arr;
    }
    //显示全部企业
    public function positionList($page)
    {
        $page = $page*5;
        //查询所有数据
        $arr =  DB::table('company as c')
            ->join('position as p', 'c.id', '=', 'p.company_id')
            ->select('c.name','c.address','c.image', 'p.position_name','p.high', 'p.low','p.education','p.work_life','p.describe','p.position_id')
            ->paginate($page)->toArray();
            return response()->json($arr);
    }
    //职位详情查询
    public function positionInfo($arr)
    {
        $id = $arr['sou'];
        //查询所有数据
        $arr =  DB::table('company as c')
            ->join('position as p', 'c.id', '=', 'p.company_id')
            ->where('p.position_id','=',"$id")
            ->get();
        return $arr;
    }
}